//
//  HMComposeCollectionView.swift
//  WeiBo
//
//  Created by 李立达 on 15/12/16.
//  Copyright © 2015年 heima11. All rights reserved.
//
import UIKit

class HMComposeCollectionView: UICollectionView ,UICollectionViewDataSource,UICollectionViewDelegate{
    
  private let HMComposePictureViewItemMargin: CGFloat = 5
  private let HMComposePictureViewItemMaxCol = 3
  private lazy var images: [UIImage] = [UIImage]()
override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
super.init(frame: frame, collectionViewLayout:UICollectionViewFlowLayout())
    backgroundColor = UIColor.redColor()
    registerClass(HMComposeCollectionCell.self, forCellWithReuseIdentifier: "cell")
        dataSource = self
        delegate = self
    }
    override func layoutSubviews() {
        super.layoutSubviews()
    let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
    let cellW = (self.width - CGFloat(HMComposePictureViewItemMargin)*2)/3
        layout.itemSize = CGSize(width: cellW, height: cellW)
        layout.minimumInteritemSpacing = 5
        layout.minimumLineSpacing = 5
    }
    func addimgaes(image:UIImage){
        images.append(image)
        reloadData()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension HMComposeCollectionView {

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let count = images.count
        return count == 0 || count == 9 ? count : count + 1
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! HMComposeCollectionCell
        
        cell.backgroundColor = randColor()
        cell.image = indexPath.row + 1 > images.count ? nil : images[indexPath.row]
        
        return cell
    }

}



class HMComposeCollectionCell:UICollectionViewCell {
   
    var image:UIImage?{
        didSet{
            if image != nil{
            imageView.image = image
            imageView.highlightedImage = nil
            }
            else
            {
                imageView.image = UIImage(named: "compose_pic_add")
                imageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
            }
            
        }
    }
    lazy var imageView: UIImageView = UIImageView()
    lazy var deletButton : UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "deleimage:", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        return button
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        
       contentView.addSubview(imageView)
       contentView.addSubview(deletButton)
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.snp_edges)
        }
    
        deletButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.trailing.equalTo(self)        }
    }
}
